package calculate;

import java.util.Arrays;

public class Binary {
	public static void main(String args[]) {
		int array[] = { 49, 38, 65, 97, 76, 13, 27 };
		binarySort(array, array.length);
		System.out.println("---------排序后的结果----------");
		System.out.println(Arrays.toString(array));
	}

	// 二分查找
	public static int binarySearch(int array[], int low, int high, int temp) {
		int mid = 0;
		while (low <= high) {
			mid = (low + high) / 2;
			if (array[mid] < temp && temp <= array[mid + 1]) {
				return (mid + 1);
			} else if (array[mid] < temp) {
				low = mid + 1;
			} else {
				high = mid - 1;
			}
		}
		return high;
	}

	// 二分排序
	public static void binarySort(int array[], int size) {
		int i, j, k, temp;
		for (i = 1; i < size; i++) {
			temp = array[i];
			if (array[i] < array[0]) {
				k = 0;
			} else {
				k = binarySearch(array, 0, i, temp);
			}

			for (j = i; j > k; j--) {
				array[j] = array[j - 1];
			}
			array[k] = temp;
			System.out.println(Arrays.toString(array));
		}
	}
}
